﻿var search = location.search.substr(1).split('&').map(function (a) {
  var s = a.split('='); return [decodeURIComponent(s[0]), decodeURIComponent(s[1])]
}).reduce(function (a, b) {
  a[b[0]] = b[1]; return a;
  }, {});



var formatDate = function (date,fmt) {
  fmt = fmt || "yyyy-MM-dd hh:mm:ss";
  var o = {
    "M+": date.getMonth() + 1, //月份
    "d+": date.getDate(), //日
    "h+": date.getHours(), //小时
    "m+": date.getMinutes(), //分
    "s+": date.getSeconds(),//秒
  };
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
  for (var k in o)
    if (new RegExp("(" + k + ")").test(fmt))
      fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1)
        ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
  return fmt;
};

function copy(o) {
  return JSON.parse(JSON.stringify(o));
}

function repeat(str, n) {
  if (n === undefined) n = 1;
  var s = "";
  while (--n >= 0) {
    s += str;
  }
  return s;
}

function round(val, radix) {
  if (val == null) return null;
  var n = 1;
  if (!radix) radix = 0;
  while (radix-- > 0) {
    n *= 10;
  }
  return Math.round(val * n) / n;
}
function round1(val) {
  return Math.round(val * 10) / 10;
}
function round2(val) {
  return Math.round(val * 100) / 100;
}
function round3(val) {
  return Math.round(val * 1000) / 1000;
}


if (!Array.prototype.findIndex) {
  Object.defineProperty(Array.prototype, 'findIndex', {
    value: function(predicate) {
     // 1. Let O be ? ToObject(this value).
      if (this == null) {
        throw new TypeError('"this" is null or not defined');
      }

      var o = Object(this);

      // 2. Let len be ? ToLength(? Get(O, "length")).
      var len = o.length >>> 0;

      // 3. If IsCallable(predicate) is false, throw a TypeError exception.
      if (typeof predicate !== 'function') {
        throw new TypeError('predicate must be a function');
      }

      // 4. If thisArg was supplied, let T be thisArg; else let T be undefined.
      var thisArg = arguments[1];

      // 5. Let k be 0.
      var k = 0;

      // 6. Repeat, while k < len
      while (k < len) {
        // a. Let Pk be ! ToString(k).
        // b. Let kValue be ? Get(O, Pk).
        // c. Let testResult be ToBoolean(? Call(predicate, T, « kValue, k, O »)).
        // d. If testResult is true, return k.
        var kValue = o[k];
        if (predicate.call(thisArg, kValue, k, o)) {
          return k;
        }
        // e. Increase k by 1.
        k++;
      }

      // 7. Return -1.
      return -1;
    }
  });
}

/**
 * 给函数一个延迟，如果在延迟时间内再次触发，则会延后调用时间（用于防止多次刷新）
 */
function DelayRun(id, handler, timeout) {
  if (timeout == null) timeout = 30;
  if (DelayRun.handlers[id]) {
    DelayRun.handlers[id].handler = handler;
    DelayRun.handlers[id].timeout = timeout;
    return DelayRun.handlers[id].proxy;
  }
  DelayRun.handlers[id] = {
    handler: handler, timeout: timeout, _this: this
  };
  handler = null; timeout = null;
  var proxy = function () {
    var id = arguments.callee._id;
    var handler = DelayRun.handlers[id];
    if (handler) {
      clearTimeout(handler.tid);
      var args = [].slice.call(arguments, 0);
      handler.tid = setTimeout(function () {
        handler.handler.apply(handler._this, args);
        delete (DelayRun.handlers[id]);
      }, handler.timeout);
    }
  }
  proxy._id = id; DelayRun.handlers[id].proxy = proxy; id = null;
  return proxy;
}
DelayRun.handlers = {};


function OnceRun(id, handler, timeout) {
  if (timeout == null) timeout = 300;
  var data = OnceRun.calltime[id];
  if (data) return data.proxy;
  data = { id: id, handler: handler, timeout: timeout, _this: this };
  OnceRun.calltime[id] = data;
  var proxy = function () {
    var data = arguments.callee.data;
    var now = +new Date();
    if (data.time) {
      if (data.timeout < 0 || now - data.time < data.timeout) return;
    }
    data.time = now;
    data.handler.apply(data._this, [].slice.call(arguments, 0));
  }
  data.proxy = proxy;
  proxy.data = data;
  proxy.handler = handler;
  handler = null; id = null; timeout = null; data = null;
  return proxy;
}
OnceRun.calltime = {};


if (!String.prototype.endsWith) {
  String.prototype.endsWith = function (search, this_len) {
    if (this_len === undefined || this_len > this.length) {
      this_len = this.length;
    }
    return this.substring(this_len - search.length, this_len) === search;
  };
}

Array.prototype.prop = function (prop, val) {
  if (val === undefined) {
    return this.map(function (a) { return a[prop] });
  } else {
    return this.filter(function (a) { return a[prop] == val });
  }
}
function ran(min,max){ //随机整数
  return Math.floor(Math.random()*(max-min+1))+min
}